/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : reserve_list.js                                 
*  Description  : 실시간 예약 리스트
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-26  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	
	// 가승인 결제 도움말
	function clsPreReserveHelpBox()
	{
		this._init();
	}
	
	clsPreReserveHelpBox.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.box_txt");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, show: function(){
			this.$getLayer().show(300);
		}
		, hide: function(){
			this.$getLayer().hide(300);
		}
		, toggle: function(){
			this.$getLayer().toggle(300);
		}
	}

	// 예약정보
	function clsReserveInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsReserveInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_view_bgc");

			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("aside.right_view ul.r_event_list li:first").clone(true)
			}
			
			this.setInfo({
				"이벤트리스트": []
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("p.golf_d span:eq(0)").html("<em>" + this.infoData["골프장명"] + "</em> " + this.infoData["코스명"]);
			$layer.find("p.golf_d span:eq(1)").html(this.infoData["홀명"]);
			$layer.find("p.res_date").contents().eq(0).before(this.infoData["날짜"] + ". " + this.infoData["요일"] + "요일").remove();
			$layer.find("p.res_date span").html(this.infoData["시간"]);
			$layer.find("div.res_price div.cost span").html(_util.number_format(this.infoData["원가"]));
			$layer.find("div.res_price div.sale p.s_price span").html(_util.number_format(this.infoData["가격"]));
			
			if(this.infoData["원가"] == this.infoData["가격"]){
				$layer.find("div.res_price div.sale p.ma_right20").html("정상그린피");
				$layer.find("div.res_price>div:lt(2)").hide();
			}
			
			
			$layer.find("p.amount strong.ft_b").html(_util.number_format(this.infoData["보증결제금액"]));
			if(this.infoData["보증결제금액"]){
				$layer.find("p.info").hide();
			}else{
				_util.executeThread(function(){
					return _gf.getMyInfoData();
				}, function(){
					$layer.find("p.info").show().find("strong.name").html(_gf.getMyInfoData()["닉네임"]);
				});
			}
			
			
			$layer.find("ul.ico_list img").each(function(n){
				var s = $(this).attr("src");
				$(this).attr("src", s.replace("_off", "").replace(".png", "_off.png"));
				if(thisClass.infoData["이벤트"]){
					if(thisClass.infoData["이벤트"].indexOf($(this).attr("alt")) != -1) $(this).attr("src", s.replace("_off", ""));
				}
			});
			
			$layer.find("p.tit_event").html(this.infoData["이벤트명"]);
			
			$layer.find("aside.right_view ul.r_event_list").empty();
			
			if(this.infoData["이벤트리스트"].length){
				$layer.find("aside.right_view article.board_list_none").hide();
				
				for(var i=0;i<this.infoData["이벤트리스트"].length;i++){
					$layer.find("aside.right_view ul.r_event_list").append($element.clone(true));
				}
				$layer.find("aside.right_view ul.r_event_list li").each(function(n){
					$(this).html(thisClass.infoData["이벤트리스트"][n]);
				});
			}else{
				$layer.find("aside.right_view article.board_list_none").show();
				$layer.find("aside.right_view ul.r_event_list").hide();
				
				$layer.find("aside.right_view p.tit_event").hide();
			}
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.reserve_view").children().hide();
			if($layer.find("div.reserve_view div#tmpSpin").length == 0){
				$layer.find("div.reserve_view").append("<div id='tmpSpin' style='position:relative; width:700px; height:150px;' />");
			}
			$layer.find("div.reserve_view div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.reserve_view").children().show();
			$layer.find("div.reserve_view div#tmpSpin").hide().spin(false);
		}
		, clone: function(){
			return this.$getLayer().clone(true);
		}
		, getPDate: function(){
			return this.infoData["가승인결제마감일"];
		}
		, getMoney: function(){
			return this.infoData["보증결제금액"];
		}
		, getAgreeYN: function(){
			return this.infoData["결제동의여부"];
		}
	}
	
	// 예약정보 탭
	function clsReserveInfoTab()
	{
		this.idx = null;
		
		this._init();
	}
	
	clsReserveInfoTab.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.tabmenu");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("li").removeClass("on");
			this.setIdx(0);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setIdx: function(idx){
			this.idx = idx;
			this.display();
		}
		, getIdx: function(){
			return this.idx;
		}
		, display: function(){
			this.$getLayer().find("li").removeClass("on").eq(this.idx).addClass("on");
		}
	}
	
	
	// 코스정보
	function clsCourseInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsCourseInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.children("div.detail_info").children("p:first, div.detail_golf:first").clone(true)
			}
			
			this.setInfo({
				"코스알림": ""
				, "코스리스트":[]
			});
			$layer.hide();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("div.detail_info:eq(0)").prev("h6").hide();
			if(this.infoData["코스알림"]) $layer.find("div.detail_info:eq(0)").prev("h6").show();
			$layer.find("div.detail_info:eq(0)").html(this.infoData["코스알림"]);
//			if(this.infoData["자세히url"]){
//				$layer.find("img[alt='코스자세히보기']").parent("a").attr({
//					"target": "_blank"
//					, "href": this.infoData["자세히url"]
//				}).parent("span.bt_detail").show();
//			}
//			else{
//				$layer.find("img[alt='코스자세히보기']").parents("span.bt_detail").hide();
//			}
			
			if(this.infoData["코스리스트"]){
				$layer.find(">div.detail_info:eq(1)").empty();
				
				for(var i=0;i<this.infoData["코스리스트"].length;i++){
					$layer.find(">div.detail_info:eq(1)").append($element.clone(true));
				}
				$layer.find(">div.detail_info:eq(1)>p").each(function(n){
					var d = thisClass.infoData["코스리스트"][n];
					$(this).find("strong").html(d["코스명"]);
					$(this).next().html('<p class="txt_info">' + d["상세정보"] + '</p>');
					$(this).next().prepend('<img src="' + _gf.filePath2UrlA(d["파일"]) + '" alt="" />').error(function(){
						$(this).remove();
					});
				});
			}
		}
		, show: function(){
			this.$getLayer().show(300);
		}
		, hide: function(){
			this.$getLayer().hide(300);
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.children().hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:700px; height:150px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.children(":not(div#tmpSpin)").show();
		}
	}
	
	
	// 가격정보
	function clsPriceInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsPriceInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"그린피주중회원": 0
				, "그린피주말회원": 0
				, "그린피주중비회원": 0
				, "그린피주말비회원": 0
				, "그린피주중주중회원": 0
				, "그린피주말주중회원": 0
				, "그린피기타": 0
				, "카트비": 0
				, "캐디비": "0 원"
				, "대여채비": 0
				, "대여화": 0
				, "코스인원제한": ""
			});
			$layer.hide();
		}
		, $getLayer: clsCourseInfo.prototype.$getLayer
		, setInfo:clsCourseInfo.prototype.setInfo
		, display: function(){
			var $layer = this.$getLayer();
			
			$layer.find("tbody tr:eq(0) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중회원"]));
			$layer.find("tbody tr:eq(0) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말회원"]));
			$layer.find("tbody tr:eq(1) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중비회원"]));
			$layer.find("tbody tr:eq(1) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말비회원"]));
			$layer.find("tbody tr:eq(2) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중주중회원"]));
			$layer.find("tbody tr:eq(2) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말주중회원"]));
			$layer.find("tbody tr:eq(3) em").html(_util.number_format(this.infoData["그린피기타"]));
			$layer.find("tbody tr:eq(4) td").html(_util.number_format(this.infoData["카트비"]) + " 원");
			$layer.find("tbody tr:eq(5) td").html(this.infoData["캐디비"]);
			$layer.find("tbody tr:eq(6) td").html(_util.number_format(this.infoData["대여채비"]) + " 원");
			$layer.find("tbody tr:eq(7) td").html(_util.number_format(this.infoData["대여화"]) + " 원");
			$layer.find("tbody tr:eq(8) td").html(this.infoData["코스인원제한"]);
		}
		, show: clsCourseInfo.prototype.show
		, hide: clsCourseInfo.prototype.hide
		, spinStart: clsCourseInfo.prototype.spinStart
		, spinEnd: clsCourseInfo.prototype.spinEnd
		
	}
	
	// 패널티 정보
	function clsPenalty()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsPenalty.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(2)");
			this._HTMLDom = {
				"$layer": $layer
			}
			$layer.hide();
		}
		, $getLayer: clsCourseInfo.prototype.$getLayer
		, show: clsCourseInfo.prototype.show
		, hide: clsCourseInfo.prototype.hide
	}
	
	// 최근 예약 목록
	function clsRecentReserveList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsRecentReserveList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(3)");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").css("cursor", "pointer").clone(true)
			}
			
			this.setList([]);
			$layer.hide();
		}
		, $getLayer: clsCourseInfo.prototype.$getLayer
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			var sValue = {
				"W": "예약대기"
				, "P": "가예약"
				, "G": "보증예약"
				, "A": "완료"
				//, "C": "취소"
				, "PC": "가예약취소"
				, "GC": "보증예약취소"
			}
			
			$layer.find("tbody").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("tbody").append($element.clone(true));
			}
			$layer.find("tbody tr").each(function(n){
				var d = thisClass.listData[n];
				
				d["가격"] = d["가격"] || d["원가"];
				
				$(this).find("td:eq(0)").html(d["날짜"]);
				$(this).find("td:eq(1)").html(d["시간"]);
				$(this).find("td:eq(2)").html(d["골프장명"] + " " + d["코스명"]);
				$(this).find("td:eq(3)").html(d["홀"]);
				$(this).find("td:eq(4) p:eq(0)").html(d["가격"]);
				$(this).find("td:eq(4) p:eq(1)").html(d["원가"]);
				
				if(d["가격"] == d["원가"]) $(this).find("td:eq(4) p:eq(1)").hide();
				
				
				$layer.find("ul.ico_list img").each(function(n){
					var s = $(this).attr("src");
					$(this).attr("src", s.replace("_off", "").replace(".png", "_off.png"));
					if(d["이벤트"]){
						if(d["이벤트"].indexOf($(this).attr("alt")) != -1) $(this).attr("src", s.replace("_off", ""));
					}
				});
				
//				$(this).find("ul.ico_list li").each(function(){
//					if(d["이벤트"] != null){
//						if(d["이벤트"].indexOf($(this).find("img").attr("alt")) == -1) $(this).find("img").hide();
//						else $(this).find("img").show();
//					}
//				});
				
				$(this).find("td:eq(6)").html(sValue[d["상태코드"]] + ((d["상태코드"] == "P") ? "<br>(보증결제대기)":""));
			});
		}
		, show: clsCourseInfo.prototype.show
		, hide: clsCourseInfo.prototype.hide
		, spinStart: clsCourseInfo.prototype.spinStart
		, spinEnd: clsCourseInfo.prototype.spinEnd
		, getKey: function(n){
			return this.listData[n]["키"];
		}
		, getAreaKey: function(n){
			return this.listData[n]["지역코드"];
		}
		, getGolfKey: function(n){
			return this.listData[n]["골프장코드"];
		}
		, getCourseKey: function(n){
			return this.listData[n]["코스코드"];
		}
	}
	
	
	// 예약정보 버튼
	function clsReserveInfoButton()
	{
		this.infoData = null;
		
		this._init();
	}
	
	clsReserveInfoButton.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.btn_area_box02");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"예약상태": ""
				, "결제등급": ""
				, "가예약가능여부": "N"
				, "보증결제가능여부": "N"
				, "날짜": ""
				, "시간": ""
			});
		}
		, $getLayer: clsCourseInfo.prototype.$getLayer
		 , setInfo:clsCourseInfo.prototype.setInfo
		 , display: function(){
		 	var $layer = this.$getLayer();
		 	
		 	$layer.find("img:not([alt='목록'])").hide();
		 	
		 	if(!this.infoData["날짜"] || !this.infoData["시간"]){
		 		return;
		 	}
		 	
		 	var aDate = this.infoData["날짜"].split(". ");
		 	var aTime = this.infoData["시간"].split(":");
		 	
		 	var theD = new Date(aDate[0], aDate[1]-1, aDate[2], aTime[0], aTime[1]);
		 	var nowD = new Date();
		 	
		 	if(theD.getTime() < nowD.getTime()) return;
		 	
		 	switch(this.infoData["예약상태"]){
		 		case "W":
		 			$layer.find("img[alt='확정예약'], img[alt='가예약']").show();
		 			break;
		 		case "P":
		 			$layer.find("img[alt='확정예약'], img[alt='가예약취소']").show();
		 			break;
		 		case "G":
		 		case "C":
		 		case "A":
		 			$layer.find("img[alt='예약취소']").show();
		 			break;
		 	}
		 	
		 	if(this.infoData["결제등급"] == "S"){
		 		$layer.find("img[alt='가예약']").hide();
		 	}
		 	
		 	
		 	if(this.infoData["보증결제가능여부"] == "Y"){
		 		$layer.find("img[alt='확정예약']").show();
		 	}else{
		 		$layer.find("img[alt='확정예약']").hide();
		 	}
		 	
		 	if(this.infoData["가예약가능여부"] == "Y"){
		 		$layer.find("img[alt='가예약']").show();
		 	}else{
		 		$layer.find("img[alt='가예약']").hide();
		 	}
		}
	}
	
	// 확정예약 가승인 결제 동의
	function clsPaymentAgree()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"NOCHECK": "동의에 체크해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPaymentAgree.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.reserve_dec_pop");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
		}
		, $getLayer: clsCourseInfo.prototype.$getLayer
		, show: function(){
			this.$getLayer().show(300);
		}
		, hide: function(){
			this.$getLayer().hide(300);
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer = this.$getLayer();
			
			this.errMsg = null;
			if(!$layer.find("input:checkbox").prop("checked")){
				this.errMsg = "NOCHECK";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this.$getLayer().find("input:checkbox").trigger("focus");
		}
	}
	
	
	
	function clsComm()
	{
		
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 예약정보
		, getReserveInfo: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "getReservView";
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, {
				"reserv_seq": arg["예약코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "결과값": {
						"예약정보": {}
						, "코스정보": {}
					}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					var reserv_date = _util.nvl(obj["reserv__date"])+"";
					var reserv_time = _util.nvl(obj["reserv__time"])+"";
					var szEvent = "";
					var pDate = new Date(reserv_date.substr(0, 4), reserv_date.substr(4, 2)-1, reserv_date.substr(6, 2));
					pDate.setDate(pDate.getDate()-5);
					
					var e = [];
					
					var amt_type = {
						"amt": "원"
						, "percent": "%"
					}
										
					if(obj["greenfee__use__yn"] == "Y"){
						szEvent = "그린피";
						e.push("그린피 할인: " + obj["greenfee__discount__amount"] +  amt_type[_util.nvl(obj["greenfee__amt__type"])]);
					}
					
//					if(obj["mileage__use__yn"] == "Y"){
//						e.push("마일리지: " + obj["mileage__amount"] +  obj["mileage__type"]);
//					}
					
					if(obj["cartfee__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "카트피";
						e.push("카트피: " + obj["cartfee__discount__amount"]);
					}
					
					if(obj["food__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "식사";
						e.push("식사: " + obj["food__discount__amount"]);
					}
					
					if(obj["present__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "선물";
						e.push("선물: " + obj["present__content"]);
					}
					
					if(obj["etc__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "기타";
						e.push("기타: " + obj["etc__content"]);
					}
					
					res["결과값"]["예약정보"] = {
						"예약코드": arg["예약코드"]
						, "코스코드": "001"
						, "골프장명": _util.nvl(obj["golfclub__name"])
						, "코스명": _util.nvl(obj["course__info__name1"])
						, "홀명": _util.nvl(obj["hole"]) + "H"
						, "날짜": reserv_date.substr(0, 4) + ". " + reserv_date.substr(4, 2) + ". " + reserv_date.substr(6, 2)
						, "요일": _util.nvl(obj["reserv__day"])
						, "시간": reserv_time.substr(0, 2) + ":" + reserv_time.substr(2, 2)
						, "원가": _util.nvl(obj["price"], 0)
						, "가격": _util.nvl(obj["greenfee"], 0) || _util.nvl(obj["price"], 0)
						, "이벤트": szEvent
						, "보증결제금액": _util.nvl(obj["price__pay"], 0)
						, "예약상태": obj["reserv__status"]
						, "결제등급": obj["reserv__paytype"]
						, "가승인결제마감일": pDate
						, "이벤트리스트": e
						, "이벤트명": _util.nvl(obj["event__name"])
						, "결제동의여부": _util.nvl(obj["pay__agree"])
						, "가예약가능여부": _util.nvl(obj["nonpay__yn"])
						, "보증결제가능여부": _util.nvl(obj["pay__yn"])
					}
					
					res["결과값"]["코스정보"] = {
						"코스알림": _util.nvl(obj["course__notice"])
						, "코스리스트": []
					}
					
					for(var i=1;i<4;i++){
						if(_util.nvl(obj["course__info__id" + i])){
							var objCourseList = {
								"코드코드": _util.nvl(obj["course__info__id" + i])
								, "코스명": _util.nvl(obj["course__info__name" + i])
								, "상세정보": _util.nvl(obj["description" + i])
							}
							
							if(obj["course" + i + "__file"]){
								objCourseList["파일"] = _util.nvl(obj["course1__file"][0]["items"]["path"]) + _util.nvl(obj["course1__file"][0]["items"]["save__file__name"])
							}
							
							res["결과값"]["코스정보"]["코스리스트"].push(objCourseList);
						}
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 가격정보
		, getPriceInfo: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}

			//ajax
			var serviceName = "getGolfclubFee";
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, {
				"region": arg["지역코드"]
				, "golfclub_id": arg["골프장코드"]
				, "course_info_id": arg["코스코드"]
				, "resev_seq": arg["예약코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"그린피주중회원": _util.nvl(obj["greenfee__weekday__member"], 0)
						, "그린피주말회원": _util.nvl(obj["greenfee__weekend__member"], 0)
						, "그린피주중비회원": _util.nvl(obj["greenfee__weekday__non__member"], 0)
						, "그린피주말비회원": _util.nvl(obj["greenfee__weekend__non__member"], 0)
						, "그린피주중주중회원": _util.nvl(obj["greenfee__weekday__weekdaymember"], 0)
						, "그린피주말주중회원": _util.nvl(obj["greenfee__weekend__weekdaymember"], 0)
						, "그린피기타": _util.nvl(obj["etc__price"], 0)
						, "카트비": _util.nvl(obj["cartfee"], 0)
						, "캐디비": _util.nvl(obj["caddiefee"])
						, "대여채비": _util.nvl(obj["rentfee"], 0)
						, "대여화": _util.nvl(obj["rental__shoes"], 0)
						, "코스인원제한": _util.nvl(obj["course__men__limit"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 최근 예약 목록
		, getRecentReserveList: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "getMyReservList";
			var options = {
				
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
				, "listcount": 100000
				, "currpageno": 1
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var reserve_date = _util.nvl(obj["reserv__date"])+"";
						var reserve_time = _util.nvl(obj["reserv__time"])+"";
						
						var szEvent = "";
					
						if(obj["greenfee__use__yn"] == "Y"){
							szEvent = "그린피";
						}
						
						if(obj["cartfee__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "카트피";
						}
						
						if(obj["food__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "식사";
						}
						
						if(obj["present__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "선물";
						}
						
						if(obj["present__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "선물";
						}
						
						if(obj["etc__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "기타";
						}
						
						res["결과값"].push({
							"키": _util.nvl(obj["reserv__seq"])
							, "날짜": reserve_date.substrEx(0, 4) + "." + reserve_date.substrEx(4, 2) + "." + reserve_date.substrEx(6, 2)
							, "시간": reserve_date.substrEx(0, 2) + ":" + reserve_date.substrEx(2, 2)
							, "골프장명": _util.nvl(obj["golfclub__name"])
							, "코스명": _util.nvl(obj["course__info__id1"])
							, "홀": _util.nvl(obj["hole"])
							, "가격": _util.nvl(obj["greenfee"], 0)
							, "원가": _util.nvl(obj["price"], 0)
							, "이벤트": szEvent
							, "상태코드": _util.nvl(obj["reserv__status"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 가예약
		, preReserveSubmit: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservAccess";
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, {
				"reserv_seq": arg["예약코드"]
				, "reserv_status": arg["예약상태"]
				, "mid": ""
				, "merchant_key": ""
				, "moid": ""
				, "goods_name": ""
				, "card_number": ""
				, "exp_year": ""
				, "exp_month": ""
				, "card_id_no": ""
				, "card_pw": ""
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 가예약취소
		, preReserveCancel: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservCancel";
			var options = {
				"reserv_seq": arg["예약코드"]
				, "reserv_status": arg["예약상태"]
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, options,  function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objBookingControl = null;
		this.objReserveInfo = null;
		this.objReserveInfoTab = null;
		this.objCourseInfo = null;
		this.objPriceInfo = null;
		this.objPenalty = null;
		this.objRecentReserveList = null;
		this.objReserveInfoButton = null;
		this.objPreReserveHelpBox = null;
		this.objPaymentAgree = null;
		
		this.q_reserve = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objBookingControl = new clsBookingControl();
			this.objReserveInfo = new clsReserveInfo();
			this.objReserveInfoTab = new clsReserveInfoTab();
			this.objCourseInfo = new clsCourseInfo();
			this.objPriceInfo = new clsPriceInfo();
			this.objPenalty = new clsPenalty();
			this.objRecentReserveList = new clsRecentReserveList();
			this.objReserveInfoButton = new clsReserveInfoButton();
			this.objPreReserveHelpBox = new clsPreReserveHelpBox();
			this.objPaymentAgree = new clsPaymentAgree();
		
			this._bind();
			this._beforeStart();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.q_reserve = _util.getParam("q_reserve");
			
//			$.ajax({
//				"url": "https://web.nicepay.co.kr/flex/js/nicepay_tr.js"
//				, type: "get"
//				, dataType: "script"
//				, success: function(ret){
//					NicePayUpdate();
//				}
//			});
			
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				"좌메뉴": false
			}
			
			_util.executeThread(function(){
				return thisClass.objBookingControl.isLoad();
			}, function(){
				thisClass._permitStart["좌메뉴"] = true;
			});
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return _gf.getMyInfoData();
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
			
			
		}
		, _bind: function(){
			var thisClass = this;
			
			// 달력 선택시
			$(this.objBookingControl).on("calendarChange", null, function(){
				thisClass.objBookingControl.goMainPage();
			});
			
			// 가예약 도움말
			this.objReserveInfo.$getLayer().on("hover", "img[alt='가승인 결제 도움말']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "가승인 결제 도움말":
						thisClass.objPreReserveHelpBox.toggle();
						break;
				}
//				switch(e.type.toUpperCase()){
//					case "MOUSEENTER":
//						thisClass.objPreReserveHelpBox.show();
//						break;
//					case "MOUSELEAVE":
//						thisClass.objPreReserveHelpBox.hide();
//						break;
//					
//				}
			});
			
			// 탭
			this.objReserveInfoTab.$getLayer().on("click", "li", function(){
				var n = thisClass.objReserveInfoTab.$getLayer().find("li").index($(this));
				
				thisClass.objReserveInfoTab.setIdx(n);
				thisClass.reserve_info_tab();
			});
			
			// 버튼
			this.objReserveInfoButton.$getLayer().on("click", "img[alt='목록'], img[alt='가예약'], img[alt='가예약취소'], img[alt='확정예약'], img[alt='예약취소']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "목록":
						var n = new Date();
						if(_util.getParam("q_date")){
							n.setTime(_util.getParam("q_date"));
						}else{
							n.setDate(n.getDate() + 1);
						}
						thisClass.objBookingControl.goReserveListPage(n);
						break;
					case "가예약": 
						thisClass.objComm.preReserveSubmit({
							"예약코드": thisClass.q_reserve
							, "예약상태": "P"
							, "콜백": function(ret){
								thisClass.finishPreReserveSubmit(ret);
							}
						});
						break;
					case "가예약취소":
						if(!confirm("가예약을 취소하시겠습니까?\n[확인]을 누르시면 예약이 취소됩니다.")) return;
						thisClass.objComm.preReserveCancel({
							"예약코드": thisClass.q_reserve
							, "예약상태": "P"
							, "콜백": function(ret){
								thisClass.finishPreReserveCancel(ret);
							}
						});
						break;
					case "확정예약":
						thisClass.confirmReserve();
						break;
					case "예약취소":
						if(!confirm("예약을 취소하시겠습니까?\n[확인]을 누르시면 예약이 취소됩니다.")) return;
						thisClass.objComm.preReserveCancel({
							"예약코드": thisClass.q_reserve
							, "예약상태": "G"
							, "콜백": function(ret){
								thisClass.finishPreReserveCancel(ret);
							}
						});
						break;
				}
			});
			
			// 가승인 결제 동의
			this.objPaymentAgree.$getLayer().on("click", "img[alt='close'], input:image", function(){
				var t = this.tagName.toUpperCase();
				
				switch(t){
					case "IMG":
						thisClass.objPaymentAgree.hide();
						break;
					case "INPUT":
						if(!thisClass.objPaymentAgree.valid()){
							alert(thisClass.objPaymentAgree.getError());
							return;
						}
						
						thisClass.objPaymentAgree.hide();
						thisClass.payStart();
				}
			});
			
			// 고객센터
			this.objReserveInfo.$getLayer().find("a:contains('고객센터')").attr({
				"id": "idPermissionLink"
				, "target": "_blank"
				, "href": _ROOT + "/launge/customer.html"
			});
			
			// 부킹정보 확인
			this.objRecentReserveList.$getLayer().find("img[alt='예약,부킹정보 확인']").parent("a").attr({
				"id": "idPermissionLink"
				, "target": "_blank"
				, "href": _ROOT + "/mypage/my_booking.html"
			});
			
			// 최근 예약내역
			this.objRecentReserveList.$getLayer().on("click", "tbody tr", function(){
				var n = thisClass.objRecentReserveList.$getLayer().find("tbody tr").index($(this));
				var k = thisClass.objRecentReserveList.getKey(n);
				var a = thisClass.objRecentReserveList.getAreaKey(n);
				var g = thisClass.objRecentReserveList.getGolfKey(n);
				var c = thisClass.objRecentReserveList.getCourseKey(n);
				
				thisClass.goReserveViewPage(k, a, g, c);
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.objBookingControl._start(this);
			
			this.objReserveInfo.spinStart();
			this.objCourseInfo.spinStart();
			this.objComm.getReserveInfo({
				"예약코드": this.q_reserve
				, "콜백": function(ret){
					thisClass.finishReserveInfo(ret);
				}
			});
			
			this.objPriceInfo.spinStart();
			this.objComm.getPriceInfo({
				"예약코드":  this.q_reserve
				, "지역코드": _util.getParam("q_area")
				, "골프장코드": _util.getParam("q_golf")
				, "코스코드": _util.getParam("q_course")
				, "콜백": function(ret){
					thisClass.finishPriceInfo(ret);
				}
			});
			
			this.objRecentReserveList.spinStart();
			this.objComm.getRecentReserveList({
				"콜백": function(ret){
					thisClass.finishRecentReserveList(ret);
				}
			});
		}
		// 예약정보 내 탭 분류
		, reserve_info_tab: function(){
			var idx = this.objReserveInfoTab.getIdx();

			var aList = [
				this.objCourseInfo
				, this.objPriceInfo
				, this.objPenalty
				, this.objRecentReserveList
			];
			
			for(var i=0;i<aList.length;i++){
				if(i == idx) aList[i].show();
				else aList[i].hide();
			}
		}
		// 예약정보 완료
		, finishReserveInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objReserveInfo.spinEnd();
			this.objReserveInfo.setInfo(ret["결과값"]["예약정보"]);
			
			this.objReserveInfoButton.setInfo({
				"결제등급": ret["결과값"]["예약정보"]["결제등급"]
				, "예약상태": ret["결과값"]["예약정보"]["예약상태"]
				, "가예약가능여부": ret["결과값"]["예약정보"]["가예약가능여부"]
				, "보증결제가능여부": ret["결과값"]["예약정보"]["보증결제가능여부"]
				, "날짜": ret["결과값"]["예약정보"]["날짜"]
				, "시간": ret["결과값"]["예약정보"]["시간"]
			});
			
			this.objCourseInfo.spinEnd();
			this.objCourseInfo.setInfo(ret["결과값"]["코스정보"]);
			
			this.objReserveInfoTab.setIdx(0);
			this.reserve_info_tab();
		}
		// 가격정보 완료
		, finishPriceInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objPriceInfo.spinEnd();
			this.objPriceInfo.setInfo(ret["결과값"]);
		}
		// 최근 예약 목록 완료
		, finishRecentReserveList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objRecentReserveList.spinEnd();
			this.objRecentReserveList.setList(ret["결과값"]);
		}
		// 가예약완료
		, finishPreReserveSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.goPreReserveFinishPage();
		}
		// 가예약취소 완료
		, finishPreReserveCancel: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			document.location.reload();
		}
		// 가예약완료 이동
		, goPreReserveFinishPage: function(){
			var szLink = _ROOT + "/booking/reserve_check.html" + _util.paramStringFilter();
			document.location.href = szLink;
		}
		// 확정예약 완료 이동
		, goReserveFinishPage: function(){
			var szLink = _ROOT + "/booking/reserve_ok.html?q_reserve=" + this.q_reserve;
			document.location.href = szLink;
		}
		// 확정 예약하기
		, confirmReserve: function(){
			var m = this.objReserveInfo.getMoney();
			var agree = this.objReserveInfo.getAgreeYN();
			
			if(m){
				if(agree == "Y"){
					this.objPaymentAgree.show();
				}else{
					this.payStart();
				}
			}else{
				alert(0);
			}
		}
		// 보증결제
		, payStart: function(){
			var winNicepay = window.open(_ROOT + "/inc/nicepay_billkey.html?q_reserve=" + this.q_reserve, "winNicepay", "width=545, height=573, scrollbars=0");
			if(winNicepay) winNicepay.focus();
		}
		// 보증결제 완료
		, closeNicepay: function(w, ret){
			if(w) w.close();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.goReserveFinishPage();
		}
		// 예약 상세 이동
		, goReserveViewPage: function(seq, area, golf, course){
			var param = {
				"q_reserve": seq
				, "q_area": area
				, "q_golf": golf
				, "q_course": course
			}
			
			
			var szLink = _ROOT + "/booking/reserve_view.html" + _util.object2paramString(param);
			$("body").append("<a href='" + szLink + "' id='idPermissionLink' target='_blank'></a>");
			$("a:last").get(0).click();
			$("a:last").remove();
		}
	}
	
	$.ajax({
		url: _ROOT + "/booking/js/booking_common.js"
		, type: "get"
		, dataType: "script"
		, async: false
		, success: function(ret){
			document.objControl = new clsControl();
			document.objControl._init();
		}
		, error: function(){
			alert("error");
		}
	});
});
